<template>
  <div>
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon @click="$router.go(-1)" name="cross" slot="left" class="left-icon" />
    </van-nav-bar>
    <van-form ref="loginForm" @submit="onSubmit">
      <van-field v-model="user.mobile" name="mobile" type="number" maxlength="11" placeholder="请输入手机号"
        :rules="userFormRules.mobile">
        <!-- <template v-slot:left-icon> -->
        <!-- <template #left-icon>
          <i class="link link-shouji"> </i>
        </template> -->
        <i slot="left-icon" class="link link-shouji"></i>
      </van-field>

      <van-field v-model="user.code" name="user.code" type="number" maxlength="6" placeholder="请输入验证码"
        :rules="userFormRules.code">
        <!-- <template v-slot:left-icon> -->
        <!-- <template #left-icon>
          <i class="link link-yanzhengma"> </i>
        </template> -->
        <i slot="left-icon" class="link link-yanzhengma"></i>

        <template #button>
          <van-count-down @finish='isCountDown=false' format="ss s" v-if="isCountDown" :time="6 * 1000" />
          <van-button v-else class="btn_send" size="small" type="primary" native-type="button" @click="clickCode">发送验证码
          </van-button>

        </template>

      </van-field>
      <div class="login-btns">
        <van-button round block type="info" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user.js'
export default {
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      isCountDown: false

    }
  },
  methods: {
    async clickCode () {
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log('发送验证码')
      } catch (err) {
        return console.log('校验失败')
      }

      this.$toast.loading({
        message: '发送中...',
        forbidClick: true,
        duration: 0
      })

      try {
        await sendSms(this.user.mobile)
        this.$toast.success('发送成功')
        this.isCountDown = true
      } catch (err) {
        if (err.response && err.response.status === 429) {
          this.$toast.fail('接口请求太频繁')
        } else {
          this.$toast.fail('发送失败，请稍后再试')
        }
      }
    },
    async onSubmit () {
      console.log(this.user)
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data: res } = await login(this.user)
        this.$store.commit('inituser', res.data)
        this.$toast.success('登录成功')
        this.$router.push('/profile')
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast.fail('账号或密码错误')
        }
        this.$toast.fail('系统异常,登录失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.link {
  font-size: 34px;
}

.btn_send {
  font-size: 22px;
  color: #666;
  border: none;
  width: 152px;
  height: 46px;
  background-color: #ededed;
  border-radius: 23px;
}
.login-btns {
  margin: 53px 28px;
  .van-button {
    width: 694px;
    height: 88px;
    background-color: #6db4fb;
    border-radius: 10px;
    border: none;
  }
}
</style>
